﻿using UnityEngine;
using System.Collections;

public class AgentSkill : MonoBehaviour {


	public Animator animator;
	protected Rigidbody rigi;

	protected NavMeshAgent agent;
	public float speed;
	public static bool _hit2;
	public static bool _dead;
	public static bool _recover;
	
	private AnimatorStateInfo state;
	
	void Start () {
		
		animator = GetComponent<Animator>();
		rigi = GetComponent<Rigidbody>();

		agent = GetComponent<NavMeshAgent>();
		
		speed = 2f;
	}
	void Update () {
		
		state = animator.GetCurrentAnimatorStateInfo(0);
		if (state.IsName("Hit.Hit2"))
		{
			_hit2 = true;
		}
		else 
		{
			_hit2 = false;
		}
		if(Input.GetKeyDown(KeyCode.A))
		{
			if (!_dead)
			{
				StartCoroutine(dead());
				_dead = true;
			}
		}
		if(Input.GetKeyDown(KeyCode.R))
		{
			if (_dead)
			{
				StartCoroutine(recover());
			}
		}
		if (Input.GetKeyDown(KeyCode.I))
		{
			if(!_dead && !_recover)
			{
				StartCoroutine(hit());
			}
		}
		
	}
	
	/// <summary>
	/// 受伤
	/// </summary>
	private IEnumerator hit()
	{
		animator.SetBool("Hit2",true);
		yield return new  WaitForSeconds(0.1f);
		animator.SetBool("Hit2",false);
	}
	/// <summary>
	/// 死亡
	/// </summary>
	private IEnumerator dead()
	{
		animator.SetBool("Dead",true);
		yield return new  WaitForSeconds(0.1f);
		animator.SetBool("Dead",false);
	}
	/// <summary>
	/// 复活
	/// </summary>
	private IEnumerator recover()
	{
		animator.SetBool("Recover",true);
		yield return new  WaitForSeconds(0.2f);
		animator.SetBool("Recover",false);
		_dead = false;
	}
	private void LateUpdate()
	{
		Camera.main.camera.transform.position = new Vector3 (this.transform.position.x,this.transform.position.y+5f,this.transform.position.z-4f);
	}
	public void JoyMove(Vector3 dir)
	{	
		animator.SetFloat("Speed",1f);
		animator.speed = 1f;
		agent.Move(dir*Time.deltaTime*0.2f);
	}
	
	private void OnAnimatorMove()
	{
		rigi.position+=animator.deltaPosition*speed;
		
	}
	/// <summary>
	/// 释放技能
	/// </param>
	public void ActionCtr(string skill)
	{
		if(skill.Equals("Combo"))
		{
			StartCoroutine(relaseCombo(skill));
		}
		else 
		{
			StartCoroutine(relaseSkill(skill));
		}
	}
	/// <summary>
	/// 技能
	/// </param>
	private IEnumerator relaseSkill(string skill)
	{
		//animator.speed = 1f;
		animator.SetBool(skill,true);
		yield return new  WaitForSeconds(0.050f);
		animator.SetBool(skill,false);
	}
	/// <summary>
	/// 普攻
	/// </param>
	private IEnumerator relaseCombo(string combo)
	{
		animator.SetBool(combo,true);
		//animator.speed = 1f;
		yield return new  WaitForSeconds(0.5f);
		animator.SetBool(combo,false);
		
	}
	

}